/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.ejb.dae.phieuthuedv;

import com.sun.rowset.WebRowSetImpl;
import java.sql.CallableStatement;
import java.sql.Connection;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.sql.rowset.WebRowSet;
import uit.ejb.connection.ConnectingSQL;
import uit.ejb.dto.PhieuThueDVDTO;

/**
 *
 * @author Restart
 */
public class PhieuThueDVDAE implements EntityBean {

    private EntityContext context;
    
    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">

    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise beans, Web services)
    // TODO Add business methods
    // TODO Add create methods

    /**
     * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
     */
    public void setEntityContext(EntityContext aContext) {
        context = aContext;
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbActivate()
     */
    public void ejbActivate() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbPassivate()
     */
    public void ejbPassivate() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbRemove()
     */
    public void ejbRemove() {
        
    }
    
    /**
     * @see javax.ejb.EntityBean#unsetEntityContext()
     */
    public void unsetEntityContext() {
        context = null;
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbLoad()
     */
    public void ejbLoad() {
        // TODO add code to retrieve data
    }
    
    /**
     * @see javax.ejb.EntityBean#ejbStore()
     */
    public void ejbStore() {
        // TODO add code to persist data
    }

    // </editor-fold>
    
    /**
     * See EJB 2.0 and EJB 2.1 section 12.2.5
     */
    public java.lang.String ejbFindByPrimaryKey(java.lang.String aKey) throws FinderException {
        // TODO add code to locate aKey from persistent storage
        // throw javax.ejb.ObjectNotFoundException if aKey is not in
        // persistent storage.
        return "";
    }

    public boolean phieuThueDV_Insert(PhieuThueDVDTO phieuthuedv) {
        try{
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_PhieuThueDV_Insert(?,?,?,?)}");
            cs.setString(1, phieuthuedv.getMaPTDV());
            cs.setString(2, phieuthuedv.getMaPhieuThue());
            cs.setString(3, phieuthuedv.getSoLuong());
            cs.setString(4, phieuthuedv.getGhiChu());
            cs.executeUpdate();
            return true;
        }catch(Exception e){
            e.getMessage();
            return false;
        }
    }

    public boolean phieuThueDV_Delete(String id) {
        try{
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_PhieuThueDV_Delete(?)}");
            cs.setString(1, id);
            cs.executeUpdate();
            return true;
        }catch(Exception e){
            e.getMessage();
            return false;
        }
    }

    public WebRowSet phieuThueDV_GetById(String id) {
        try{
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_PhieuThueDV_GetById(?)}");
            cs.setString(1, id);
            WebRowSet wrs = new WebRowSetImpl();
            wrs.populate(cs.executeQuery());
            return wrs;
        }catch(Exception e){
            e.getMessage();
            return null;
        }
    }

    public WebRowSet phieuThueDV_GetByTop(String top, String where, String order) {
        try{
            Connection _con = ConnectingSQL.makeConnection();
            CallableStatement cs = _con.prepareCall("{call sp_PhieuThueDV_GetByTop(?,?,?)}");
            cs.setString(1, top);
            cs.setString(2, where);
            cs.setString(3, order);
            WebRowSet wrs = new WebRowSetImpl();
            wrs.populate(cs.executeQuery());
            return wrs;
        }catch(Exception e){
            e.getMessage();
            return null;
        }
    }

}
